package team.cdlzz.service;

import com.alibaba.fastjson.JSONObject;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.cdlzz.dao.CodeQuestionMapper;
import team.cdlzz.dao.CodeSubmitMapper;
import team.cdlzz.dao.ExamMapper;
import team.cdlzz.dao.QuestionMapper;
import team.cdlzz.model.pojo.CodeQuestion;
import team.cdlzz.model.pojo.CodeSubmit;
import team.cdlzz.model.pojo.Exam;
import team.cdlzz.model.pojo.Question;
import team.cdlzz.util.IDUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zw
 * @date 2022-06-27.
 */
@Service
public class QuestionService {
    @Autowired
    public CodeQuestionMapper codeQuestionMapper;

    @Autowired
    public CodeSubmitMapper codeSubmitMapper;

    @Autowired
    public QuestionMapper questionMapper;
    @Autowired
    public ExamMapper examMapper;
    @Autowired
    public RunService runService;

    public String getStaticCode(){
        return "#include <iostream>\n" +
                "\n" +
                "using namespace std;\n" +
                "\n" +
                "int main () {\n" +
                "    int a, b;\n" +
                "    cin >> a >> b;\n" +
                "    cout << a + b << endl;\n" +
                "    return 0;\n" +
                "}";
    }

    public String setExcelFile(String cid) {
        return "";
    }

    /**
     * 查询所有题目
     * @return
     */
    public List<CodeQuestion> getCodeDataListAll(){
       return codeQuestionMapper.selectCodeQuestionAll();
    }

    /**
     * 查询单个题目
     * @param id
     * @return
     */
    public CodeQuestion getCodeData(Integer id){
        return codeQuestionMapper.selectByPrimaryKey(id);
    }

    public JSONObject SubmitCode(CodeSubmit codeSubmit) throws IOException {
        CodeQuestion codeQuestion = codeQuestionMapper.selectByPrimaryKey(codeSubmit.getCid());//获取到题目信息
        JSONObject jsonObject = runService.RunResultMap(codeQuestion, codeSubmit);
        CodeSubmit submit = (CodeSubmit) jsonObject.get("submit");
        SetSubmit(submit); //执行更新数据库操作
        return jsonObject;
    }

    public Double DuplicateChecking(Integer cid,Integer uid){
        List<CodeSubmit> codeSubmits =  codeSubmitMapper.selectyAll();
        CodeSubmit codeSubmit = codeSubmitMapper.selectSubmit(cid,uid);
        double f =0;
        for (CodeSubmit submit : codeSubmits) {
            if (submit.getUid()==codeSubmit.getUid()){
                System.out.println("跳过自己的答案");
                continue;
            }
            Double aDouble = IDUtils.DuplicateChecking(submit.getAnswer(), codeSubmit.getAnswer());
            f = f>aDouble?f:aDouble;
            System.out.println(submit.getUid()+" 用户与宁的查重为 "+ aDouble);
        }
        return f;
    }

    /**
     * 如果满分则更新数据库,修改为最新的(存在则更新,不存在插入)
     * @param codeSubmit
     */
    public void SetSubmit(CodeSubmit codeSubmit){
        CodeSubmit codeSubmit1 = codeSubmitMapper.selectSubmit(codeSubmit.getCid(), codeSubmit.getUid());
//        System.out.println(codeSubmit.toString()+"\n"+codeSubmit1.toString());
        if (codeSubmit.getScore()==100){
            System.out.println("本次代码为100分,将更新之前的信息");
            if (codeSubmit1!=null){
                codeSubmit.setId(codeSubmit1.getId());
                int in  = codeSubmitMapper.updateByPrimaryKey(codeSubmit);
                System.out.println("更新了"+in+"条信息");
            }else {
                codeSubmitMapper.insert(codeSubmit);
            }
        }
    }

    public List<Question> getExamQuestionList(Integer eid) {
        Exam exam = examMapper.selectByPrimaryKey(eid);
        List<Question> questionList = questionMapper.selectBy();
        int size = questionList.size();
        ;
        int[] arraysNorepeat = getArraysNorepeat(0, size, exam.getNum());
        List list = new ArrayList();
        for (int i : arraysNorepeat) {
            list.add(questionList.get(i));
        }
        return list;
    }


    /**
     * 获取不重复的随机数组
     * @param min 随机数范围最小值
     * @param max 随机数范围最大值
     * @param amount  随机数个数
     * @return 传入的随机数个数长度的数组
     * 因为随机数不可重复，所以最大值减去最小值不能小于随机数个数
     */
    public static int[] getArraysNorepeat(int min,int max,int amount){
        if((max - min) < amount){
            throw new RuntimeException("参数异常：随机个数大于随机范围");
        }
        int arr[] = new int[amount];
        int ran,i=0;
        boolean repeat;//记录是否存在重复
        while(i < arr.length){
            repeat = false;
            ran = (int) (Math.random()*(max-min)+min);
            for (int j = 0; j < arr.length; j++) {
                if( arr[j] == ran){
                    repeat = true;
                    break;
                }
            }
            if(!repeat){
                arr[i] = ran;
                i++;
            }
        }
        return arr;
    }

}
